package com.cloudera.oryx.example

import java.util

import com.cloudera.oryx.ml.param.{HyperParamValues, HyperParams}
import com.typesafe.config.{Config, ConfigFactory}
import org.junit.{After, Before, Test}
import org.slf4j.{Logger, LoggerFactory}

import scala.collection.mutable.ListBuffer

class TestLoadConfig {
  private val log:Logger = LoggerFactory.getLogger("TestLoadConfig")

  @Before
  def init(): Unit ={
    log.info("init")
    System.setProperty("config.file",getResourcePath("oryx-lr.conf"))
    log.info("config file: "+ System.getProperty("config.file"))
  }

  @After
  def destroy(): Unit ={
    log.info("destroy")
  }

  @Test
  def loadConfig(): Unit ={
    val config = ConfigFactory.defaultApplication
    val hyperParamValues: util.List[HyperParamValues[_]] = new util.ArrayList[HyperParamValues[_]]()
    val hyperList = ListBuffer(
      HyperParams.fromConfig(config, "oryx.lr.hyperparams.maxIter"),
      HyperParams.fromConfig(config, "oryx.lr.hyperparams.regParam"),
      HyperParams.fromConfig(config, "oryx.lr.hyperparams.elasticNetParam"))
    import scala.collection.JavaConversions._
    hyperParamValues.addAll(hyperList)

    val maxIter = hyperParamValues.get(0)
    val regParam = hyperParamValues.get(1)
    val elasticNetParam = hyperParamValues.get(2)
    println(maxIter.getTrialValues(1))
    println(regParam.getTrialValues(1))
    println(elasticNetParam.getTrialValues(1))
    println(elasticNetParam.getTrialValues(2))
  }

  private def getResourcePath(fileName:String): String ={
    val tmpPath = this.getClass.getResource("/").getPath
    val rootPath = tmpPath.substring(1, tmpPath.indexOf("/target"))
    val main = "/src/main/resources/"
    return rootPath.concat(main).concat(fileName)
  }

}
